package com.cea.papyrus.diagram.composite.helper;

import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.InstanceSpecification;
import org.eclipse.uml2.uml.InstanceValue;
import org.eclipse.uml2.uml.PrimitiveType;
import org.eclipse.uml2.uml.Slot;

import com.cea.papyrus.core.utils.Debug;
import com.cea.papyrus.diagram.classdiagram.helper.NodeSlotHelper;
import com.cea.papyrus.diagraminterchange2.di2.Diagram;
import com.cea.papyrus.diagraminterchange2.di2.GraphElement;
import com.cea.papyrus.diagraminterchange2.di2.Uml1SemanticModelBridge;

public class CompositeSlotHelper extends NodeSlotHelper {

	/**
	 * 
	 * 
	 * @param parent 
	 * 
	 * @return 
	 */
	@Override
	public boolean canCreateGraphElement(GraphElement parent){

		if (!(parent instanceof Diagram)) {//the parent must be a graphNode

			if(parent.getSemanticModel()!=null){ //it is linked to an UML element
				Element elt= ((Uml1SemanticModelBridge)parent.getSemanticModel()).getElement();

				if ((elt instanceof InstanceSpecification)){ //this is the first container of a classifier.
					return true;
				}
				if ((elt instanceof Slot && (!(((Slot)elt).getDefiningFeature().getType() instanceof PrimitiveType)))){ //this is the first container of a classifier.
					return true;
				}
			}
		}
		return false;
	}

	@Override
	public  org.eclipse.uml2.uml.Element createUMLElement(String name,  org.eclipse.uml2.uml.Element umlParent){
		Debug.debug(this,"Call NodeEnumerationLiteralFactory.createElement()", channel); //$NON-NLS-1$
		org.eclipse.uml2.uml.Slot aSlot=null;

		if (umlParent instanceof Slot && (!(((Slot)umlParent).getDefiningFeature().getType() instanceof PrimitiveType))){
			if(((Slot)umlParent).getValues().get(0)!=null){
				InstanceValue theInstanceValue=((InstanceValue)((Slot)umlParent).getValues().get(0));
				
				aSlot=((InstanceSpecification)theInstanceValue.getInstance()).createSlot();
				return aSlot;
			}
		}
		else if (umlParent instanceof org.eclipse.uml2.uml.InstanceSpecification){
			aSlot=((org.eclipse.uml2.uml.InstanceSpecification)umlParent).createSlot();
		}
		return aSlot;
	}
}
